package com.risenb.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.risenb.bean.Permission;
import com.risenb.bean.ResultMsg;
import com.risenb.service.PermissionService;

@Controller
@RequestMapping("/permission")
public class PermissionController extends CustomerBaseControllor {
	
	@Autowired
	PermissionService permissionService;

	@RequestMapping("/list")
	public ModelAndView findPermission(Integer mid) {
		ModelAndView mv = new ModelAndView("manage/permission");
		List<Permission> ps = permissionService.queryAll();
		mv.addObject("mid", mid);
		mv.addObject("permissions", ps);
		return mv;
	}

	@RequestMapping("/fp")
	public ModelAndView findMenusByParentId(Integer parentid) {
		List<Permission> ms = permissionService.queryAll();
		List<Permission> ms2 = permissionService.queryPermissionByParentId(parentid);
		return new ModelAndView("/manage/permission").addObject("permissions", ms).addObject("menubodys", ms2).addObject("parentid", parentid);
	}

	/**
	 * 进入修改页面
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("mod")
	public ModelAndView modPermissions(Integer id, HttpServletRequest request, HttpServletResponse response) {
		ModelAndView model = new ModelAndView("manage/permission-mod");
		Permission permission = permissionService.queryPermissionById(id);
		model.addObject("permission", permission);
		return model;
	}

	/**
	 * 进入添加页面
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("add")
	public ModelAndView addPermissions(Integer parentid, HttpServletRequest request, HttpServletResponse response) {
		ModelAndView model = new ModelAndView("manage/permission-add");
		model.addObject("parentid", parentid);
		return model;
	}

	/**
	 * 修改保存
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/update")
	public void updatePermissions(Integer id, HttpServletRequest request, HttpServletResponse response) throws IOException {
		String title = request.getParameter("title");
		String sort = request.getParameter("sort");
		if(null == sort || "".equals(sort)){
			sort="0";
		}
		String url = request.getParameter("url");
		String ismenu = request.getParameter("ismenu");
		if(null == ismenu || "".equals(ismenu)){
			ismenu="0";
		}

		Permission permission = new Permission();
		permission.setId(id);
		permission.setUrl(url);
		permission.setTitle(title);
		permission.setIsmenu(Integer.valueOf(ismenu));
		permission.setSort(Integer.parseInt(sort));

		int row = permissionService.modifyPermissionById(permission);

		ResultMsg resultMsg = null;
		if (row > 0) {
			resultMsg = new ResultMsg("200", "操作完成", "", "permission", "", "", "操作完成", true);
		} else {
			resultMsg = new ResultMsg("300", "操作失败", "", "permission", "", "", "操作失败", true);
		}
		returnJson(resultMsg);
	}

	/**
	 * 添加保存
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/save")
	public void savePermissions(Integer id, HttpServletRequest request, HttpServletResponse response) throws IOException {
		String title = request.getParameter("title");
		String parentid = request.getParameter("parentid");
		String url = request.getParameter("url");
		
		if(null == parentid || "".equals(parentid)){
			parentid="0";
		}
		String sort = request.getParameter("sort");
		if(null == sort || "".equals(sort)){
			sort="0";
		}
		String ismenu = request.getParameter("ismenu");
		if(null == ismenu || "".equals(ismenu)){
			ismenu="0";
		}
		Permission permission = new Permission();
		permission.setTitle(title);
		permission.setUrl(url);
		permission.setOperatime(new Date());
		permission.setOperator(1);
		permission.setIsmenu(Integer.parseInt(ismenu));
		permission.setStatus(0);
		permission.setParentid(Integer.parseInt(parentid));
		permission.setSort(Integer.parseInt(sort));

		int row = permissionService.createPermission(permission);

		ResultMsg resultMsg = null;
		if (row > 0) {
			resultMsg = new ResultMsg("200", "操作完成", "", "permission", "", "", "操作完成", true);
		} else {
			resultMsg = new ResultMsg("300", "操作失败", "", "permission", "", "", "操作失败", true);
		}
		returnJson(resultMsg);
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/del")
	public void delPermissions(Integer id, HttpServletRequest request, HttpServletResponse response) throws IOException {
		Permission permission = new Permission();
		permission.setId(id);
		int row = permissionService.removePermissionById(id);

		ResultMsg resultMsg = null;
		if (row > 0) {
			resultMsg = new ResultMsg("200", "操作完成", "", "permission", "", "", "操作完成");
		} else {
			resultMsg = new ResultMsg("300", "操作失败", "", "permission", "", "", "操作失败");
		}
		returnJson(resultMsg);
	}

}
